package thread;

/**
 * Created with IntelliJ IDEA.
 * ClassName: Demo
 * Package: thread
 * Description:
 * User: fzykd
 *
 * @Author: LQH
 * Date: 2023-10-10
 * Time: 10:19
 */

class My extends Thread{
    @Override
    public void run() {
        System.out.println("创建子类继承Thread");
    }
}

class Me implements Runnable{
    @Override
    public void run() {
        System.out.println("创建一个类 实现Runnable接口");
    }
}

//创建线程的多种方式
public class Demo {
    public static void main(String[] args) {
        Thread t = new My();
        t.start();

        //将Runnable实例传入
        Thread t1 = new Thread(new Me());
        t1.start();

        //匿名内部类写法
        Thread t2 = new Thread(){
            @Override
            public void run() {
                System.out.println("匿名内部类");
            }
        };
        t2.start();

        Thread t3 = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("Runnable匿名内部类");
            }
        });
        t3.start();

        //lambda表达式创建
        Thread t4 = new Thread(()->{
            //不用重写run方法
            System.out.println("lambda创建线程");
        });
        t4.start();
    }
}
